package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.ejia.pojo.car;
import com.ejia.pojo.fytype;
import com.ejia.pojo.money;
import com.ejia.pojo.notice;
import com.ejia.pojo.user;
import com.ejia.server.AdminService;
import com.ejia.server.CarServices;
import com.ejia.server.MoneyService;
import com.ejia.server.UserService;

import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.TextArea;
import javax.swing.JTextPane;
import java.awt.SystemColor;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;

public class Mainuser extends JFrame {

	private JPanel contentPane;
	private static JTable table;
	private static ArrayList<money> moneys;
	private static ArrayList<fytype> fytypes;
	private static ArrayList<notice> notices;
	private static ArrayList<car> cars;
	private static int j=0;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Mainuser frame = new Mainuser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public static void dateBind() {
		ArrayList<user> users= new ArrayList<>();
		users= UserService.findRealname(login.cname);
		String name=users.get(0).getRealname();
		moneys = MoneyService.WYJFMXfind(name);
		Object[][] data =new Object[moneys.size()][];
		for (int i = 0; i < data.length; i++) {
			if (moneys.get(i).getStatus()==0) {
					data[i]=new Object[] {
					moneys.get(i).getMid(),
					moneys.get(i).getTypeid().getFyname(),
					moneys.get(i).getNumber(),
					moneys.get(i).getTypeid().getUnit(),
					moneys.get(i).getTypeid().getFyprice(),
					moneys.get(i).getStartDate(),
					moneys.get(i).getEndDate(),
					"未缴费"
				};
			}else {
				data[i]=new Object[] {
						moneys.get(i).getMid(),
						moneys.get(i).getTypeid().getFyname(),
						moneys.get(i).getNumber(),
						moneys.get(i).getTypeid().getUnit(),
						moneys.get(i).getTypeid().getFyprice(),
						moneys.get(i).getStartDate(),
						moneys.get(i).getEndDate(),
						"已缴费"
				};
			}
			
		}
		table.setModel(new DefaultTableModel(
				data,
				new String[] {
					"\u7F16\u53F7", "\u7C7B\u578B", "\u7528\u91CF", "\u5355\u4EF7", "\u5E94\u7F34\u8D39\u7528", "\u8D77\u59CB\u65E5\u671F", "\u7EC8\u6B62\u65E5\u671F", "\u72B6\u6001"
				}
			));
		table.setRowHeight(40);
		table.setFont(new Font("宋体", Font.PLAIN, 26));
	}
	
	
	public static void dataBindUser() {
		
	}
	
	
	/**
	 * Create the frame.
	 */
	public Mainuser() {
		
		Timer timer = new Timer();
		
		
		
		
		ArrayList<user> users= new ArrayList<>();
		users= UserService.findRealname(login.cname);
		String name=users.get(0).getRealname();
		cars=CarServices.find(name);
		setTitle("业主  【"+name+"】     欢迎您");
		
		
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1406, 871);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(new LineBorder(new Color(255, 147, 0)), "\u8F66\u8F86\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, new Font("宋体", 0, 48), new Color(0, 120, 215)));
		panel_3.setBounds(197, 140, 919, 466);
		contentPane.add(panel_3);
		panel_3.setLayout(null);
		
		
		JLabel label_2 = new JLabel("车牌号：未登记");
		if (!cars.get(j).getCarno().equals("")) {
			label_2.setText("车牌号：	"+cars.get(j).getCarno());
		}
		label_2.setFont(new Font("宋体", Font.PLAIN, 28));
		label_2.setBounds(84, 86, 263, 40);
		panel_3.add(label_2);
		
		JLabel label_3 = new JLabel("车位号：未登记");
		if (!cars.get(j).getCarnumber().equals("")) {
			label_3.setText("车位号：	"+cars.get(j).getCarnumber());
		}
		label_3.setFont(new Font("宋体", Font.PLAIN, 28));
		label_3.setBounds(84, 212, 263, 40);
		panel_3.add(label_3);
		
		JLabel label_4 = new JLabel("状态：未登记");
		if (!cars.get(j).getStatu().equals("")) {
			label_4.setText("状态：	"+cars.get(j).getStatu());
		}
		label_4.setFont(new Font("宋体", Font.PLAIN, 28));
		label_4.setBounds(84, 338, 263, 40);
		panel_3.add(label_4);
		
		JButton button_8 = new JButton("");
		button_8.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/向右.png")));
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object data= new Object[cars.size()][];
				if (j<cars.size()-1) {
					j++;
					label_2.setText("车牌号：	"+cars.get(j).getCarno());
					label_3.setText("车位号：	"+cars.get(j).getCarnumber());
					label_4.setText("状态：	"+cars.get(j).getStatu());
				}
			}
		});
		
		JButton button_7 = new JButton("");
		button_7.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/向左.png")));
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Object data= new Object[cars.size()][];
				if (j>0) {
					j--;
					label_2.setText("车牌号：	"+cars.get(j).getCarno());
					label_3.setText("车位号：	"+cars.get(j).getCarnumber());
					label_4.setText("状态：	"+cars.get(j).getStatu());
				}
				
			}
		});
		button_7.setFont(new Font("宋体", Font.PLAIN, 24));
		button_7.setBounds(509, 385, 123, 66);
		panel_3.add(button_7);
		button_8.setFont(new Font("宋体", Font.PLAIN, 24));
		button_8.setBounds(716, 385, 123, 66);
		panel_3.add(button_8);
		
		JLabel label_5 = new JLabel("");
		label_5.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/su7_4_1_clear.png")));
		label_5.setBounds(509, 141, 326, 197);
		panel_3.add(label_5);
		
		JLabel label_8 = new JLabel("车辆照片：");
		label_8.setFont(new Font("宋体", Font.PLAIN, 28));
		label_8.setBounds(509, 86, 140, 40);
		panel_3.add(label_8);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(255, 147, 0)), "\u65B0\u95FB\u516C\u544A", TitledBorder.LEADING, TitledBorder.TOP, new Font("宋体", 0, 48), new Color(0, 120, 215)));
		panel_2.setBounds(15, 85, 1354, 611);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		
		
		
		JComboBox comboBox = new JComboBox();
		
				notices=AdminService.notices("");
				Object[][] data1= new Object[notices.size()][];
				for (int i = 0; i < data1.length; i++) {
					data1[i]=new Object[] {
						notices.get(i).getTitle()
					};
				}
				String[] data = new String[notices.size()];
				for (int i = 0; i < data1.length; i++) {
					data[i]=(String) data1[i][0];
				}
		
		comboBox.setModel(new DefaultComboBoxModel(data));
		comboBox.setFont(new Font("宋体", Font.PLAIN, 36));
		comboBox.setBounds(422, 79, 471, 59);
		panel_2.add(comboBox);
		
		
		JTextPane txtpnAsdf = new JTextPane();
		txtpnAsdf.setBackground(SystemColor.control);
		txtpnAsdf.setFont(new Font("宋体", Font.PLAIN, 32));
		txtpnAsdf.setContentType("text/html");
		
		
		JLabel label_1 = new JLabel("新闻标题");
		label_1.setFont(new Font("宋体", Font.PLAIN, 42));
		label_1.setBounds(188, 79, 197, 59);
		panel_2.add(label_1);
		txtpnAsdf.setText("新闻公告");
		txtpnAsdf.setEditable(false);
		txtpnAsdf.setBounds(15, 185, 1324, 411);
		panel_2.add(txtpnAsdf);
		
		JButton button_6 = new JButton("查询");
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int nid=AdminService.nid((String)comboBox.getSelectedItem());
				String nr=AdminService.notices(nid);
				txtpnAsdf.setText(nr);
				txtpnAsdf.setFont(new Font("宋体", Font.PLAIN, 32));
			}
		});
		button_6.setFont(new Font("宋体", Font.PLAIN, 42));
		button_6.setBounds(942, 79, 242, 59);
		panel_2.add(button_6);
		
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(15, 85, 1354, 611);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JButton button_5 = new JButton("缴费");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRowCount()>0) {
					int index=table.getSelectedRow();
					int id=moneys.get(index).getMid();
					if (MoneyService.update(id)) {
						dateBind();
						JOptionPane.showMessageDialog(null,"缴费成功");
					}else {
						JOptionPane.showMessageDialog(null,"缴费失败");
					}
					
					
				}else {
					JOptionPane.showMessageDialog(null, "请先选择用户");
				}
			}
		});
		button_5.setFont(new Font("宋体", Font.PLAIN, 42));
		button_5.setBounds(1131, 31, 208, 73);
		panel_1.add(button_5);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(15, 140, 1324, 456);
		panel_1.add(scrollPane);
		
		table = new JTable();
		dateBind();
		scrollPane.setViewportView(table);
		
		JPanel panel = new JPanel();
		panel.setBounds(148, 85, 1088, 611);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel label = new JLabel("");
		label.setBounds(0, 0, 1312, 611);
		panel.add(label);
		label.setHorizontalAlignment(SwingConstants.LEFT);
		label.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/766943d9627d474a816a59799e382e70_0.png")));
		
		
		panel.show();
		panel_1.hide();
		panel_2.hide();
		panel_3.hide();
		
		
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 1384, 66);
		contentPane.add(menuBar);
		menuBar.setMargin(new Insets(15, 0, 15, 0));
		
		JMenu mnNewMenu = new JMenu("账户管理");
		mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
		mnNewMenu.setFont(new Font("宋体", Font.PLAIN, 42));
		mnNewMenu.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/我的账单.png")));
		menuBar.add(mnNewMenu);
		
		JMenu mnNewMenu_1 = new JMenu("查看信息");
		mnNewMenu_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new UserInfo().setVisible(true);
				dispose();
			}
		});
		mnNewMenu_1.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/查看信息.png")));
		mnNewMenu_1.setFont(new Font("宋体", Font.PLAIN, 42));
		mnNewMenu.add(mnNewMenu_1);
		
		JMenu menu_4 = new JMenu("修改密码");
		menu_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new XGMM().setVisible(true);
				dispose();
			}
		});
		menu_4.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/修改密码.png")));
		menu_4.setFont(new Font("宋体", Font.PLAIN, 42));
		mnNewMenu.add(menu_4);
		
		JMenu menu = new JMenu("物业缴费");
		menu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				panel.hide();
				panel_1.show();
				panel_2.hide();
				panel_3.hide();
			}
		});
		menu.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/缴费.png")));
		menu.setHorizontalAlignment(SwingConstants.CENTER);
		menu.setFont(new Font("宋体", Font.PLAIN, 42));
		menuBar.add(menu);
		
		JMenu menu_1 = new JMenu("新闻公告");
		menu_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				panel.hide();
				panel_1.hide();
				panel_2.show();
				panel_3.hide();
			}
		});
		menu_1.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/新闻.png")));
		menu_1.setHorizontalAlignment(SwingConstants.CENTER);
		menu_1.setFont(new Font("宋体", Font.PLAIN, 42));
		menuBar.add(menu_1);
		
		JMenu menu_2 = new JMenu("车位车辆");
		menu_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				panel.hide();
				panel_1.hide();
				panel_2.hide();
				panel_3.show();
			}
		});
		menu_2.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/车位.png")));
		menu_2.setHorizontalAlignment(SwingConstants.CENTER);
		menu_2.setFont(new Font("宋体", Font.PLAIN, 42));
		menuBar.add(menu_2);
		
		JMenu menu_3 = new JMenu("返回首页");
		menu_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				panel.show();
				panel_1.hide();
				panel_2.hide();
				panel_3.hide();
			}
		});
		menu_3.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/24gf-home9.png")));
		menu_3.setHorizontalAlignment(SwingConstants.CENTER);
		menu_3.setFont(new Font("宋体", Font.PLAIN, 42));
		menuBar.add(menu_3);
		
		JMenu menu_5 = new JMenu("退出");
		menu_5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				new login().setVisible(true);
				dispose();
			}
		});
		menu_5.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/退出.png")));
		menu_5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 42));
		menuBar.add(menu_5);
		
		
		
		
		
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date date = new Date(System.currentTimeMillis());
		JLabel label_6 = new JLabel(""+date);
		label_6.setFont(new Font("宋体", Font.PLAIN, 28));
		label_6.setBounds(959, 747, 410, 66);
		contentPane.add(label_6);
		
		JLabel label_7 = new JLabel("");
		label_7.setBounds(0, 69, 1473, 791);
		contentPane.add(label_7);
		label_7.setIcon(new ImageIcon(Mainuser.class.getResource("/com/ejia/photo/未标题-1.png")));
		
		
		
		class UpdateTask extends TimerTask {
            @Override
            public void run() {
            	SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
            	Date date = new Date();
            	label_6.setText(""+formatter.format(date));
            }
		}
		UpdateTask task = new UpdateTask();
		timer.schedule(task, 0, 1000);
		
		
		setLocationRelativeTo(null);
	}
}
